package model;

import static model.LostFilm.*;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public final class Parser
{

	
	
	public static ArrayList<Serial> getListOfSerials()
	{
		String url = SERIALS;
		Document doc = null;
		try
		{
			doc = Jsoup.connect(url).get();
			
			ArrayList<Serial> serials = new ArrayList<Serial>();
			
			System.out.println(doc.title());
			System.out.println();
			Elements eserials = doc.select("div.mid div.bb a");
			
			for (Element es : eserials)
			{
				Serial serial = new Serial();
				
				serial.name = es.ownText();
				serial.originalName = es.select("span").text();
				serial.link = es.attr("href");
				serial.id = Integer.parseInt(serial.link.replaceAll("\\D", ""));
				
				serials.add(serial.id, serial);
			}
			
			return serials;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println("Error: Cannot connect to " + url + "!");
		}
		
		return null;
	}
	
	public static Serial getSerialInfo(int id)
	{
		String url = BROWSE + id;
		Document doc = null;
		Serial serial = new Serial();
		
		try
		{
			doc = Jsoup.connect(url).get();
			
			Element eserial = doc.select("div.mid div").first();
			String sText = eserial.text();
			
			int iCountry = sText.indexOf(T_COUNTRY),
				iYear    = sText.indexOf(T_YEAR),
				iGenre   = sText.indexOf(T_GENRE),
				iCount   = sText.indexOf(T_COUNT_OF_SEASONS),
				iStatus  = sText.indexOf(T_STATUS),
				iSite    = sText.indexOf(T_SITE),
				iRuSite  = sText.indexOf(T_RU_SITE),
				iForum   = sText.indexOf(T_FORUM),
				iAbout   = sText.indexOf(T_ABOUT);
			
			String sname = eserial.select("h1").text().trim();
			int i = sname.indexOf("(");
			serial.name = sname.substring(0, i).trim();
			serial.originalName = sname.substring(i+1, sname.length()-1).trim();
			
			serial.link           = url;
			serial.imglink        = eserial.select("img").attr("src");
			
			serial.country        = getPropertyValue(sText.substring(iCountry, iYear));
			serial.year           = getPropertyValue(sText.substring(iYear, iGenre));
			serial.genre          = getPropertyValue(sText.substring(iGenre, iCount));
			serial.countOfSeasons = getPropertyValue(sText.substring(iCount, iStatus));
			serial.status         = getPropertyValue(sText.substring(iStatus, iSite));
			serial.site           = getPropertyValue(sText.substring(iSite, iRuSite));
			serial.ruSite         = getPropertyValue(sText.substring(iRuSite, iForum));
			serial.forum          = getPropertyValue(sText.substring(iForum, iAbout));
			
			serial.about          = eserial.select("span").last().text();
			ArrayList<Episode> episodes = serial.content;
			
			Element econtent = doc.select("div.mid div").get(3);
			Elements elistOfSeries = econtent.select("div.t_row");
			for (Element eseries : elistOfSeries)
			{
				Elements erows = eseries.select("tr");
				Element erowInfo = erows.get(0),
						erowName = erows.get(1);
				
				Elements ecolumns = erowInfo.select("td");
				Elements ecolDateNum = ecolumns.get(1).select("span.micro span");
				String date = ecolDateNum.get(0).ownText(),
					   number  = ecolDateNum.get(1).ownText();
				Elements ecolComments = ecolumns.get(2).select("span.micro");
				String mark      = ecolComments.select("label span b").text(),
					   markCount = ecolComments.select("label").attr("title"),
					   commentsCount  = ecolComments.select(":root > span b").text();
				
				Element enames = erowName.select("div nobr").first();
				String name = enames.select("span").text(),
					   originalName = enames.ownText();
				
				Episode episode = new Episode();
				episode.name          = name;
				episode.originalName  = originalName;
				episode.date          = date;
				episode.number        = number;
				episode.mark          = mark;
				episode.markCount     = markCount;
				episode.commentsCount = commentsCount;
				
				String[] num = episode.number.trim().split("сезон", 2);
				num[0] = num[0].replaceAll("\\D", "");
				num[1] = num[1].replaceAll("\\D", "");
				switch (num[1].length())
				{
					case 0:
						num[1] = "99";
						break;
					case 1:
						num[1] = "0" + num[1];
						break;
				}
				episode.id = Integer.parseInt(num[0]+num[1]);
				
//				if (episode.id > episodes.get(episodes.size()-1).id)
//					;
			}
			return serial;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println("Error: Cannot connect to " + url + "!");
		}
		
		return serial;
	}
	
	public static String getPropertyValue(String s)
	{
		return s.split(":", 2)[1].trim();
	}
	
	public static void main(String[] args)
	{
//		ArrayList<Serial> serials = getListOfSerials(); // 116 151
		Serial serial = getSerialInfo(151);
		serial.print();
	}
	
}
